/*******************************************************************************
 * @file Copyright 2009 The CREATE Lab at CMU
 * 
 *       This file is part of JubJub, an open source project.
 *       http://code.google.com/p/jubjub/
 * 
 *       Redistribution and use in source and binary forms, with or without
 *       modification, are permitted provided that the following conditions are
 *       met:
 * 
 *       Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 * 
 *       Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 * 
 *       Neither the name of Carnegie Mellon nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 * 
 *       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *       HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;

public class DraggableBlock
{
   private PApplet parent; // The parent PApplet that we will render ourselves onto
   private PImage img; //The image to use for the background
   private PFont font;
   private String displayText; //The text to display on the block
   private int myID;
   private int x, y;
   private int dispWidth, dispHeight;
   private int startX, startY;
   private int startMouseX, startMouseY;
   private GeneralPath gp;
   private boolean pressed;
   
   DraggableBlock(int blockID, PApplet p, String imgLoc, String txt, int myX, int myY)
   {
      myID = blockID;
      gp = new GeneralPath();
      parent = p;
      img = p.loadImage(imgLoc);
      font = p.loadFont(System.getProperty("user.dir")+"\\data\\ComicSansMS-22.vlw");
      displayText = txt;
      x = myX;
      y = myY;
      pressed = false;
      dispWidth = img.width*2;
      dispHeight = img.height*2;
      drawShape();
   }
   
   public void display() {
      parent.image(img, x, y, dispWidth,dispHeight);
      parent.textFont(font);
      parent.fill(0,255);
      parent.textAlign(parent.CENTER);
      parent.text(displayText, x+dispWidth/2, y+dispHeight/2);
      if(gp.contains(parent.mouseX,parent.mouseY))
      {
         parent.text("Inside!",400,400);
      }
    }
   
   public boolean contains(int x, int y)
   {
      return gp.contains(x,y);
   }
   
   public void press(int mouseX, int mouseY)
   {
      pressed = true;
      startMouseX = mouseX;
      startMouseY = mouseY;
      startX = x;
      startY = y;
   }
   
   public void release()
   {
      pressed = false;
   }
   
   public boolean isPressed()
   {
      return pressed;
   }
   
   public void drag(int mouseX, int mouseY)
   {
      if(pressed)
      {
         
         int xDist = mouseX-startMouseX;
         int yDist = mouseY-startMouseY;
         //System.out.println("mx = " + mouseX + " smx = " + startMouseX);         
         x = startX + xDist;
         y = startY + yDist;
         drawShape();
      }
   }
   
   private void drawShape()
   {
      gp.reset();
      gp.moveTo(x, y);
      gp.lineTo(x+dispWidth, y);
      gp.lineTo(x+dispWidth, y+dispHeight);
      gp.lineTo(x, y+dispHeight);
      gp.lineTo(x, y);
   }

}
